package dilmaj.server;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;

import org.w3c.dom.Document;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import dilmaj.client.CommentJDO;
import dilmaj.client.TermJDO;
import dilmaj.client.TermService;
import dilmaj.shared.CommentVO;
import dilmaj.shared.TermVO;

public class TermServiceImpl extends RemoteServiceServlet implements
		TermService {

	@Override
	public String[] getAllTerms() {
		// TODO Auto-generated method stub
		try {
	        Connection conn     = null;
	        String url          = "jdbc:mysql://localhost:3306/dilmaj";
	        String db           = "tms";
	        String driver       = "com.mysql.jdbc.Driver";
	        String user         = "dilmaj";
	        String pass         = "dilmaj";

			Class.forName(driver).newInstance();
			conn = DriverManager.getConnection(url, user, pass);
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery("select * from term");
						
			return ObjectFactory.convertToStringArray(rs);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				
			} catch (Exception ignore) {
				return null;
			}
		}
	}

	public List<TermVO> getAllJDOTerms() {
		ArrayList<TermVO> termVOs=new ArrayList<TermVO>();
		
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + TermJDO.class.getName();
	    List<TermJDO> termJDOs = (List<TermJDO>) pm.newQuery(query).execute();
	    
	    Iterator iterator=termJDOs.iterator();
	    while (iterator.hasNext()) {
	    	TermJDO currentTerm=(TermJDO)iterator.next();
	    	TermVO termVO=new TermVO(currentTerm);
	    	termVOs.add(termVO);
	    }
	    
	    pm.close();
	    return termVOs;
	}
	
	public static List<TermVO> getAllStaticJDOTerms() {
		ArrayList<TermVO> termVOs=new ArrayList<TermVO>();
		
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + TermJDO.class.getName();
	    List<TermJDO> termJDOs = (List<TermJDO>) pm.newQuery(query).execute();
	    
	    Iterator iterator=termJDOs.iterator();
	    while (iterator.hasNext()) {
	    	TermJDO currentTerm=(TermJDO)iterator.next();
	    	TermVO termVO=new TermVO(currentTerm);
	    	termVOs.add(termVO);
	    }
	    
	    pm.close();
	    return termVOs;
	}
	
	public TermVO update(TermVO termVO) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + TermJDO.class.getName()+" where id=="+termVO.getId();
	    List<TermJDO> termJDOs = (List<TermJDO>) pm.newQuery(query).execute();
	    TermJDO termJDO=termJDOs.get(0);
	    
	    //termJDO.setCaption(termVO.getCaption());
	    termJDO.setRank(termVO.getRank());
	    termJDO.setAreaIDs(termVO.getAreas());
	    termJDO.setSuggestion(termVO.getSuggestion());
	    termJDO.setUseCase(termVO.getUseCase());
	    Long privilege=termVO.getPrivilege();
	    if (privilege==null)
	    	termJDO.setPrivilege(0);
	    else
	    	termJDO.setPrivilege(termVO.getPrivilege());
	    termJDO.setUsers(termVO.getUsers());
	    termJDO.setComments(termVO.getComments());
	    
        try {
            pm.makePersistent(termJDO);
        } finally {
            pm.close();
        }
        
		return termVO;
	}

	public TermVO addTerm(TermVO newTerm) {
        Long userId=(Long)getThreadLocalRequest().getSession().getAttribute("userid");
        String userName=(String)getThreadLocalRequest().getSession().getAttribute("username");
        newTerm.setUserId(userId);
        newTerm.setUserName(userName);
        TermJDO termJDO = new TermJDO(newTerm);

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(termJDO);
        } finally {
            pm.close();
        }
        
        newTerm.setId(termJDO.getId());
        
        return newTerm;
	}

	public List<TermVO> search(String termPattern) {
		ArrayList<TermVO> termVOs=new ArrayList<TermVO>();
		
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + TermJDO.class.getName()+" where caption.contains(\""+termPattern+"\")";
	    List<TermJDO> termJDOs = (List<TermJDO>) pm.newQuery(query).execute();
	    
	    Iterator iterator=termJDOs.iterator();
	    while (iterator.hasNext()) {
	    	TermJDO currentTerm=(TermJDO)iterator.next();
	    	TermVO termVO=new TermVO(currentTerm);
	    	termVOs.add(termVO);
	    }
	    
	    pm.close();
	    return termVOs;
	}
}